package com.flexrrb.utils
{
	import flash.display.DisplayObject;
	
	import mx.core.IUIComponent;
	import mx.managers.ISystemManager;
	
	public class Utils
	{
		public function Utils()
		{
		}
		
		public static function makeEmailClickable(text:String):String
		{
			var regEx:RegExp = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/g;
			var matches:Array = regEx.exec(text);
			
			if(matches)
			{
				for (var i:int = 0; i < matches.length; i++)
				{
					text = text.replace(matches[i], "<u><a href='event:email-" + matches[i] + "'>" + matches[i] + "</a></u>");
				}
			}
			
			return text;
		}
		
		public static function makePhoneNumberClickable(text:String, regEx:RegExp=null):String
		{
			if(!regEx)
				regEx = /\+65\-[0-9]{8}/g;
				
			var matches:Array = regEx.exec(text);
			
			if(matches)
			{
				for (var i:int = 0; i < matches.length; i++)
				{
					text = text.replace(matches[i], "<u><a href='event:phone-" + matches[i] + "'>" + matches[i] + "</a></u>");
				}
			}
			
			return text;
		}
		
		public static function makeURLClickable(text:String, regEx:RegExp=null):String
		{
			if(!regEx)
				regEx = /((ht|f)tp(s?)\:\/\/)?[\w]+\.+[\w\/\_\-\#\:\?\;&]+((\.)+([\w]{2,5})){1,2}+((\/|\#|\.)+[\w]*)*+/g; //new RegExp("/((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/", "g");
			
			var matches:Array = text.match(regEx);
			if(matches)
			{
				for (var i:int = 0; i < matches.length; i++)
				{
					text = text.replace(matches[i], "<u><a href='event:url-" + matches[i] + "'>" + matches[i] + "</a></u>");
				}
			}
			
			return text;
		}
		
		public static function getSystemManager(target:DisplayObject):ISystemManager
	     {
	        return target is IUIComponent ?
	               IUIComponent(target).systemManager :
	               null;
	     }
				
	}
}